package org.example.demo3.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.example.demo3.dto.MessageDto;
import org.example.demo3.service.MessageService;
import org.example.demo3.vo.messageVo;
import org.example.demo3.vo.result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/message")
@RequiredArgsConstructor
@Slf4j
public class MessageController {
    @Autowired
    private MessageService messageService;

    @PostMapping(value = "/add", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public result addMessage(
            @ModelAttribute MessageDto messageDto,
            @RequestParam(value = "image", required = false) MultipartFile[] files
    ) throws IOException {
        messageService.addMessage(messageDto,files);
        return result.success();
    }

    @PostMapping("/list")
    public result<List<messageVo>>listmessage( @RequestBody MessageDto messagedto) {
        List<messageVo>p =  messageService.listmessage(messagedto);
       return result.success(messageService.listmessage(messagedto));
    }


    @Delete("/delete")
    public result delete(@Validated @RequestBody MessageDto messagedto) {
        if(!messageService.delete(messagedto)) {
            return result.fail("删除失败");
        }
        return result.success();

    }


}
